<?php
namespace app\admin\controller;

use think\facade\App;
use think\facade\Cache;
use think\Request;
use think\Db;
use think\Validate;
use think\File;

class Subject extends Base
{
    protected function getarea()
    {
        $areaarr = array(
            '371601' => '市直管',
            '371602' => '滨城区',
            '371604' => '开发区',
            '371605' => '高新区',
            '371606' => '北海新区',
            '371621' => '惠民县',
            '371622' => '阳信县',
            '371623' => '无棣县',
            '371624' => '沾化区',
            '371625' => '博兴县',
            '371626' => '邹平市',
            '371627' => '工业园区'
        );
        return $areaarr;
    }

    public function _initialize() {
        // 初始化用户信息可以参考admin模块下的Base去自行处理
    }

    public function index()
    {
        $params = ["equipment"=>"001111", "studentId"=>"10001ddd"];
        $post_data =["data"=>$params];
        return $this->buildFailed(ReturnCode::SUCCESS, $post_data);
    }

    // 添加项目信息
    public function addsubjectInfo(Request $request)
    {
        // 获取传过来的data数据
        $data= $request->param();
        foreach ($data as $k=>$v){
            $data[$k]=ltrim($data[$k],' \t\n\r\0\x0B');
            $data[$k]=str_replace(array("\r\n", "\r", "\n","\t"), "", $data[$k]);
        }
        
        if(!empty($data)) {
            // 插入数据
            $userId = Db::name('subject_info')->fetchSql(false)->insertGetId($data);
            
            if ($userId > 1) {
                return $this->buildSuccess($userId, '提交成功', ReturnCode::SUCCESS);
            } else {
                return $this->buildFailed(ReturnCode::LOGIN_ERROR, '', $userId);
            }
        }else{
            return $this->buildFailed(ReturnCode::LOGIN_ERROR,'','没有数据信息');
        }
    }

    // 获取安装设备项目表中的项目信息列表
    public function subjectListInfo(Request $request)
    {
        // 获取传过来的data数据
        $data= $request->param();
        $where[] = ['subject_del', '=', 0];
        
        if(empty($data)){
            $where[] = ['subject_name', 'like', '%'];
        }else{
            $where[]=['subject_name', 'like', '%'.$data['subject_name'].'%'];
        }

        $res= Db::table('subject_info')
            ->where($where)->fetchSql(false)
            ->select();
            
        if(!empty($res)) {
            foreach ($res as $k => $v) {
                $res_data[$k]['value'] = $v['subject_name'];
                $res_data[$k]['address'] = $v['subject_num'];
                $res_data[$k]['area'] = $v['subject_area'];
            }
            return $this->buildSuccess($res_data, '', ReturnCode::SUCCESS);
        }else{
            return $this->buildFailed(ReturnCode::LOGIN_ERROR,'','没有数据信息');
        }
    }

    // 其他方法继续在这里添加...
    // 由于代码较长，这里只展示部分，完整代码需要按照相同模式继续整理
}